﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Cinser.Common;
using System.Xml;

namespace Cinser.DataTranslate.SettingManager
{
    public class ImportSettings:SettingBase
    {
        private Cinser.Common.Model.SdeConnection sde;
        private Cinser.Common.Model.FtpConnection ftp;
        string sourceDataPath, sourceDbPassword;

        public string SourceDbPassword
        {
            get { return sourceDbPassword; }
            set { sourceDbPassword = value; }
        }
        public string SourceDataPath
        {
            get { return sourceDataPath; }
            set { sourceDataPath = value; }
        }

        public Cinser.Common.Model.SdeConnection Sde
        {
            get { return sde; }
            set { sde = value; }
        }

        public Cinser.Common.Model.FtpConnection Ftp
        {
            get { return ftp; }
            set { ftp = value; }
        }

        public ImportSettings()
        {
            Sde = new Cinser.Common.Model.SdeConnection();
            Ftp = new Cinser.Common.Model.FtpConnection();
        }

        public override void SaveSettings()
        {
            string xmlPath = SettingsManager.GetImportSettingsPath();
            Cinser.Common.XmlHelper xml = new Cinser.Common.XmlHelper(xmlPath);
            if (xml.FirstNode.FindNode("Item", "Id", this.Id, false) != null)
            {
                xml.FirstNode.UpdateNode("Item", new List<string>() { "Id" }, new List<string>() { this.Id }, false, new XmlUtils.XmlNodeHandler((node) =>
                {
                    //update
                    node.SetNodeAttribute("Id", this.Id);
                    node.SetNodeAttribute("SettingName", this.SettingName);
                    XmlNode sdeNode = node.SelectSingleNode("Sde");
                    XmlNode ftpNode = node.SelectSingleNode("Ftp");
                    XmlNode sourceNode = node.SelectSingleNode("Source");
                    sdeNode.SetNodeAttribute("Ip", this.Sde.Ip);
                    sdeNode.SetNodeAttribute("Name", this.Sde.Name);
                    sdeNode.SetNodeAttribute("Password", this.Sde.Password);
                    sdeNode.SetNodeAttribute("ServerName", this.Sde.ServerName);
                    sdeNode.SetNodeAttribute("UserName", this.Sde.UserName);
                    sdeNode.SetNodeAttribute("Version", this.Sde.Version);

                    ftpNode.SetNodeAttribute("Ip", this.Ftp.Ip);
                    ftpNode.SetNodeAttribute("Password", this.Ftp.Password);
                    ftpNode.SetNodeAttribute("Port", this.Ftp.Port.ToString());
                    ftpNode.SetNodeAttribute("UserName", this.Ftp.UserName);

                    sourceNode.SetNodeAttribute("Path", this.SourceDataPath);
                    sourceNode.SetNodeAttribute("Password", this.SourceDbPassword);
                }));
                xml.XmlDocument.Save(xmlPath);
            }
            else
            {
                //add
                XmlNode item = xml.XmlDocument.CreateElement("Item") as XmlNode;
                item.SetNodeAttribute("Id", this.Id);
                item.SetNodeAttribute("SettingName", this.SettingName);

                XmlNode sdeNode = xml.XmlDocument.CreateElement("Sde") as XmlNode;
                XmlNode ftpNode = xml.XmlDocument.CreateElement("Ftp") as XmlNode;
                XmlNode sourceNode = xml.XmlDocument.CreateElement("Source") as XmlNode;
                sdeNode.SetNodeAttribute("Ip", this.Sde.Ip);
                sdeNode.SetNodeAttribute("Name", this.Sde.Name);
                sdeNode.SetNodeAttribute("Password", this.Sde.Password);
                sdeNode.SetNodeAttribute("ServerName", this.Sde.ServerName);
                sdeNode.SetNodeAttribute("UserName", this.Sde.UserName);
                sdeNode.SetNodeAttribute("Version", this.Sde.Version);

                ftpNode.SetNodeAttribute("Ip", this.Ftp.Ip);
                ftpNode.SetNodeAttribute("Password", this.Ftp.Password);
                ftpNode.SetNodeAttribute("Port", this.Ftp.Port.ToString());
                ftpNode.SetNodeAttribute("UserName", this.Ftp.UserName);

                sourceNode.SetNodeAttribute("Path", this.SourceDataPath);
                sourceNode.SetNodeAttribute("Password", this.SourceDbPassword);

                item.AppendChild(sdeNode);
                item.AppendChild(ftpNode);
                item.AppendChild(sourceNode);

                xml.FirstNode.AppendChild(item);
                xml.XmlDocument.Save(xmlPath);
            }
        }
        
        public override string GetSettingPath()
        {
            return SettingsManager.GetImportSettingsPath();
        }
    }
}
